﻿

using LeaderCCSLaserUI.Services;
using MotionRobot.Models;
using Prism.Commands;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;

namespace LeaderCCSLaserUI.ViewModels.Dialogs
{
    public class PLCDialogViewModel : DialogViewModel
    {
        #region 变量
        private readonly IPLCService pLC;
        private CancellationTokenSource source;
        #endregion
        #region 属性
        private ObservableCollection<bool> xCoils;

        public ObservableCollection<bool> XCoils
        {
            get { return xCoils; }
            set { SetProperty(ref xCoils, value); }
        }
        private ObservableCollection<bool> yCoils;
        public ObservableCollection<bool> YCoils
        {
            get { return yCoils; }
            set { SetProperty(ref yCoils, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> yCoilsCommand;
        public DelegateCommand<object> YCoilsCommand =>
            yCoilsCommand ?? (yCoilsCommand = new DelegateCommand<object>(ExecuteYCoilsCommand));

        void ExecuteYCoilsCommand(object obj)
        {
            if (pLC.Connected)
            {                
                pLC.WriteYCoil(int.Parse(obj.ToString()) + 16, YCoils[int.Parse(obj.ToString())]);
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            Title = "PLC";
            if (pLC.Connected)
            {
                source = new CancellationTokenSource();
                CancellationToken token = source.Token;
                Task.Run(() => RunAction(token), token);
            }
        }
        public override void OnDialogClosed()
        {
            if (source != null)
            {
                source.Cancel();
            }
        }
        #endregion
        #region 构造函数
        public PLCDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            pLC = containerProvider.Resolve<IPLCService>("PLC");
            XCoils = new ObservableCollection<bool>();
            for (int i = 0; i < 12; i++)
            {
                XCoils.Add(false);
            }
            YCoils = new ObservableCollection<bool>();
            for (int i = 0; i < 8; i++)
            {
                YCoils.Add(false);
            }
        }
        #endregion
        #region 功能函数
        private void RunAction(CancellationToken token)
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
                XCoils[0] = pLC.XCoils_X0[0];
                XCoils[1] = pLC.XCoils_X0[1];
                XCoils[2] = pLC.XCoils_X0[2];
                XCoils[3] = pLC.XCoils_X0[3];
                XCoils[4] = pLC.XCoils_X0[4];
                XCoils[5] = pLC.XCoils_X0[5];
                XCoils[6] = pLC.XCoils_X0[6];
                XCoils[7] = pLC.XCoils_X0[7];

                XCoils[8] = pLC.XCoils_X0[24];
                XCoils[9] = pLC.XCoils_X0[25];
                XCoils[10] = pLC.XCoils_X0[26];
                XCoils[11] = pLC.XCoils_X0[27];

                YCoils[0] = pLC.YCoils_Y20[0];
                YCoils[1] = pLC.YCoils_Y20[1];
                YCoils[2] = pLC.YCoils_Y20[2];
                YCoils[3] = pLC.YCoils_Y20[3];
                System.Threading.Thread.Sleep(200);
            }
        }
        #endregion
    }
}
